package com.xhh.nphm.framework.datasource;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.xhh.nphm.common.constant.Constants;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


/**
 * @description: 配置多数据源
 * @author: sun
 * @date: 2023-12-24 10:31
 * @version 1.0
 */
@Configuration
public class DynamicDataSourceConfig {
    /**
     * @Primary 该注解表示在同一个接口有多个实现类可以注入的时候，默认选择哪一个
     * @param
     * @return
     * @throws SQLException
     */
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.druid.general")
    @Bean(name = "general_datasource")
    public DataSource general() throws SQLException {
        return new DruidDataSource();
    }

    @ConfigurationProperties(prefix="spring.datasource.druid.gaoxin")
    @Bean(name = "gaoxin_datasource")
    public DataSource gaoxin() throws SQLException {
        return new DruidDataSource();
    }

    /**
     * @Qualifier 根据名称进行注入，通常是在具有相同的多个类型的实例的一个注入（例如有多个DataSource类型的实例）
     * @param dataSource1    数据源1
     * @param dataSource2   数据源2
     * @return
     */
    @Bean(name = "dynamicDataSource")
    public DataSourceDynamic dataSource(@Qualifier(Constants.GENERAL_DS) DataSource dataSource1,
                                        @Qualifier(Constants.GAOXIN_DS) DataSource dataSource2){
        Map<Object, Object> dataSourceMap = new HashMap<>(2);
        dataSourceMap.put(Constants.GENERAL_DS,dataSource1);
        dataSourceMap.put(Constants.GAOXIN_DS,dataSource2);

        DataSourceDynamic dataSourceDynamic =new DataSourceDynamic();
        //该方法是AbstractRoutingDataSource的方法
        dataSourceDynamic.setTargetDataSources(dataSourceMap);
        //根据实际需求，指定默认操作的库
        dataSourceDynamic.setDefaultTargetDataSource(dataSource1);
        return dataSourceDynamic;
    }

    /**
     * 根据数据源创建SqlSessionFactory
     * @param ds
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSourceDynamic ds) throws Exception {
        // SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        //使用 mybatis plus 配置
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        VFS.addImplClass(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factory.setTypeAliasesPackage("com.xhh.nphm.**.domain");
        factory.setMapperLocations(resolver.getResources("classpath:mapper/**/*Mapper.xml"));
        factory.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
        //指定数据源(这个必须有，否则报错)
        factory.setDataSource(ds);
        return factory.getObject();
    }

}

